package com.peng.ivos.user.controller;

import com.peng.ivos.user.pojo.dto.UserLoginDTO;
import com.peng.ivos.base.response.JsonResult;
import com.peng.ivos.base.response.StatusCode;
import com.peng.ivos.user.pojo.dto.UserQuery;
import com.peng.ivos.user.pojo.dto.UserRegDTO;
import com.peng.ivos.user.pojo.vo.UserVO;
import com.peng.ivos.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.List;

/**
 * 用户控制层
 * @author： Peng
 * @date： 2024/5/20
 * @version: 1.0
 */
@RestController
@RequestMapping("/v1/user")
@Slf4j
@Api(tags = "员工管理")
public class    UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录接口
     * @return UserVO
     */
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public JsonResult login(@Validated UserLoginDTO userLoginDTO) {
        if (userLoginDTO == null){
            log.error("用户输入不能为空");
            return JsonResult.error(StatusCode.DATA_UNEXISTS,"用户输入不能为空");
        }
        return userService.getUserByUsername(userLoginDTO);
    }

    /**
     *  用户注册接口
     * @param userRegDTO 用户注册DTO
     * @return UserVO
     */
    @ApiOperation("员工注册")
    @PostMapping("/insert")
    public JsonResult addUser(UserRegDTO userRegDTO){
        return userService.addUser(userRegDTO);
    }

    /**
     * 查询用户通用接口
     * @param userQuery
     * @return List<UserVO>
     */
    @GetMapping("/select")
    @ApiOperation("员工列表查询")
    public JsonResult selectUserByExample(UserQuery userQuery){
        log.debug("userQuery:{}",userQuery);
        List<UserVO> userVOList = userService.selectUserByExample(userQuery);
        return JsonResult.success("查询成功",userVOList);
    }

    /**
     *  重置密码
     * @param id 用户ID
     */
    @ApiOperation("重置密码")
    @PostMapping("/update/password/{id}")
    public JsonResult resetPassword(@PathVariable("id") Long id){
        log.debug("重置密码，id:{}",id);
        return userService.resetPassword(id);
    }

    /**
     *  删除员工
     * @param id 员工ID
     * @return
     */
    @ApiOperation("删除员工")
    @PostMapping("/delete/{id}")
    public JsonResult deleteUserById(@PathVariable("id") Long id){
        return userService.deleteUserById(id);
    }

    /**
     *  修改员工状态
     * @param id 员工ID
     * @param status 员工状态
     * @return
     */
    @ApiOperation("修改员工状态")
    @PostMapping("/update/status/{id}/{status}")
    public JsonResult updateUserStatus(@PathVariable("id") Long id,@PathVariable("status") String status){
        log.debug("updateUserStatus,id:{},status:{}",id,status);
        return userService.updateUserStatus(id,status);
    }

    @GetMapping("/select/audit/{parentId}")
    public JsonResult selectAudit(@PathVariable Long parentId){
        log.debug("selectAudit,userQuery:{}",parentId);
        return userService.selectAuditList(parentId);
    }


}
